import java.util.*;
public class Test {
    //错误的  示范
    public static int minJuli(String str1,String str2,String[] strs) {
        if(str1.equals(str2)) {
            return 0; //两个字符串相同说明最小距离就是0  重叠
        }

        if(str1 == null || str2 == null) {  //有一个为空都返回-1
            return -1;
        }

        int left = -1, right= -1;  //left 记录str1最近的位置  right记录str2最近的位置
        int min = Integer.MAX_VALUE;
        int len = strs.length;
        for(int i = 0; i < len;i++) {
            if(strs[i].equals(str1)) {
                if(right == -1) {
                    right =min;
                }else{
                    right = i - right;
                }
                min = Math.min(min,right);
                left = i;
            }

            if(strs[i].equals(str2)) {
                if(left == -1) {
                    left = min;
                }else{
                    left = i - left;
                }
                min = Math.min(min,left);
                right = i;
            }
        }

        //这里返回的时候还要判断一下  那种一直找不到str1 或者str2其中一个的那种情况  找不到 就返回-1
        if(min == Integer.MAX_VALUE) {
            return -1;
        }else
            return min;
    }
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        String[] strs = new String[n];
        for(int i = 0;i < n;i++) {
            strs[i] = in.next();
        }
        int min = minJuli(str1,str2,strs);
        System.out.println(min);

    }

    //正确的   求两个字符串在字符串数组中的最小距离
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case

            int len = in.nextInt();
            String[] strs = new String[len];
            String str1 = in.next();
            String str2 = in.next();
            List<Integer> list_str1 = new ArrayList<Integer>();
            List<Integer> list_str2 = new ArrayList<Integer>();
            for (int i = 0; i < len; i++) {
                strs[i] = in.next();
            }
            for (int i = 0; i < len; i++) {
                String s = strs[i];
                if(str1.equals(s)) {
//                hashMap.put(s,i);
                    list_str1.add(i);
                }
                if (str2.equals(s)) {
//                hashMap.put(s,i);
                    list_str2.add(i);
                }
            }
        /*System.out.println(list_str2);
        System.out.println(list_str1);*/
            if (list_str1.isEmpty() || list_str2.isEmpty()) {
                System.out.println("-1");

            }else {
                int min =  len;
                for (int i = 0; i < list_str1.size(); i++) {
                    for (int j = 0; j < list_str2.size(); j++) {
                        int tmp  =  Math.abs(list_str1.get(i) - list_str2.get(j));
                        if (min > tmp) {
                            min = tmp;
                        }
                    }
                }
                System.out.println(min);
            }
        }
    }


        public static void main4(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            int n = in.nextInt();
            int[] cost = new int[n];
            for(int i = 0;i < n; i++) {
                cost[i] = in.nextInt();
            }
            //设置一个动态规划数组
            int[] dp = new int[n+1];
            dp[0] = 0;
            dp[1] = 0;
            for(int i = 2;i <= n;i++) {
                dp[i] = Math.min(dp[i-1] + cost[i-1],dp[i-2] + cost[i-2]);

            }
            System.out.println(dp[n]);
        }
   //牛牛送快递
    public static void main5(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        float a = in.nextFloat();
        char b = in.next().charAt(0);
        int sum = 0;
        int i = 0;  //超出的公斤数
        float prevPrice = 20f;   //一开始的价格
        if(a > 1) {
            if((a-1.0)!= (int)(a-1.0)) {
                i=(int)(a-1.0) +1;
            }else{
                i = (int)(a - 1.0);
            }
            if(b == 'y') {
                sum = 20 + i + 5;
            }else{
                sum = 20 + i;
            }
        }else if(a <= 1.0) {
            if(b == 'y') {
                sum = 20 + 5;
            }else{
                sum = 20;
            }
        }
        System.out.println(sum);
    }

    public static void main(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
    }
}
